package com.example.hao.qqwechat;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.example.hao.qqwechat.adapter.WeChatFragmentPagerAdapter;
import com.example.hao.qqwechat.fragment.DiscoverFragment;
import com.example.hao.qqwechat.fragment.LinkManFragment;
import com.example.hao.qqwechat.fragment.MeFragment;
import com.example.hao.qqwechat.fragment.MsgFragment;

import static android.support.v4.view.ViewPager.SCROLL_STATE_DRAGGING;
import static android.support.v4.view.ViewPager.SCROLL_STATE_IDLE;
import static android.support.v4.view.ViewPager.SCROLL_STATE_SETTLING;


public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {

    private static final String TAG = "MainActivity";
    private ViewPager vp_container;
    private View ll_msg;
    private View ll_linkMan;
    private View ll_discover;
    private View ll_me;
    private ImageView iv_msg;
    private ImageView iv_msg_empty;
    private ImageView iv_link_man;
    private ImageView iv_link_man_empty;
    private ImageView iv_discover;
    private ImageView iv_discover_empty;
    private ImageView iv_me;
    private ImageView iv_me_empty;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    public static Fragment[] fragments = new Fragment[]{new MsgFragment(), new LinkManFragment(), new DiscoverFragment(), new MeFragment()};

    private void initView() {

        vp_container = findViewById(R.id.vp_container);
        WeChatFragmentPagerAdapter weChatFragmentPagerAdapter = new WeChatFragmentPagerAdapter(getSupportFragmentManager(), fragments);
        vp_container.setAdapter(weChatFragmentPagerAdapter);
        vp_container.addOnPageChangeListener(this);

        ll_msg = findViewById(R.id.ll_msg);
        ll_linkMan = findViewById(R.id.ll_linkMan);
        ll_discover = findViewById(R.id.ll_discover);
        ll_me = findViewById(R.id.ll_me);


        initTabListener();

        initTabs();
    }

    private void initTabListener() {
        ll_msg.setOnClickListener(this);
        ll_linkMan.setOnClickListener(this);
        ll_discover.setOnClickListener(this);
        ll_me.setOnClickListener(this);
    }

    private void initTabs() {

        iv_msg = findViewById(R.id.iv_msg);
        iv_msg_empty = findViewById(R.id.iv_msg_empty);
        iv_link_man = findViewById(R.id.iv_link_man);
        iv_link_man_empty = findViewById(R.id.iv_link_man_empty);
        iv_discover = findViewById(R.id.iv_discover);
        iv_discover_empty = findViewById(R.id.iv_discover_empty);
        iv_me = findViewById(R.id.iv_me);
        iv_me_empty = findViewById(R.id.iv_me_empty);
    }

    @Override
    public void onClick(View v) {
        initTabsAlpha();
        initTabsVisibility();
        switch (v.getId()) {
            case R.id.ll_msg:
                vp_container.setCurrentItem(0, false);
                isSelected(iv_msg, iv_msg_empty, true);
                isSelected(iv_discover, iv_discover_empty, false);
                isSelected(iv_link_man, iv_link_man_empty, false);
                isSelected(iv_me, iv_me_empty, false);
                break;
            case R.id.ll_linkMan:
                vp_container.setCurrentItem(1, false);
                isSelected(iv_msg, iv_msg_empty, false);
                isSelected(iv_discover, iv_discover_empty, false);
                isSelected(iv_link_man, iv_link_man_empty, true);
                isSelected(iv_me, iv_me_empty, false);
                break;
            case R.id.ll_discover:
                vp_container.setCurrentItem(2, false);
                isSelected(iv_msg, iv_msg_empty, false);
                isSelected(iv_discover, iv_discover_empty, true);
                isSelected(iv_link_man, iv_link_man_empty, false);
                isSelected(iv_me, iv_me_empty, false);
                break;
            case R.id.ll_me:
                vp_container.setCurrentItem(3, false);
                isSelected(iv_msg, iv_msg_empty, false);
                isSelected(iv_discover, iv_discover_empty, false);
                isSelected(iv_link_man, iv_link_man_empty, false);
                isSelected(iv_me, iv_me_empty, true);
                break;
        }

    }


    public void initTabsVisibility() {
        isSelected(iv_msg, iv_msg_empty, true);
        isSelected(iv_link_man, iv_link_man_empty, false);
        isSelected(iv_discover, iv_discover_empty, false);
        isSelected(iv_me, iv_me_empty, false);
    }

    public void initTabsAlpha() {
        iv_msg.setAlpha(1f);
        iv_msg_empty.setAlpha(1f);
        iv_link_man.setAlpha(1f);
        iv_link_man_empty.setAlpha(1f);
        iv_discover.setAlpha(1f);
        iv_discover_empty.setAlpha(1f);
        iv_me.setAlpha(1f);
        iv_me_empty.setAlpha(1f);
    }

    public void isSelected(View nomal, View empty, boolean bool) {
        if (bool) {
            nomal.setVisibility(View.VISIBLE);
            empty.setVisibility(View.INVISIBLE);
        } else {
            nomal.setVisibility(View.INVISIBLE);
            empty.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        Log.d(TAG, "onPageScrolled: " + position + ">>>" + positionOffset + ">>>" + positionOffsetPixels);
        //1 - positionOffset 1~0 positionOffset 0~1
        switch (position) {
            case 0:
//                    Log.w(TAG, "onPageScrolled: >>0");

                iv_msg_empty.setVisibility(View.VISIBLE);
                iv_link_man.setVisibility(View.VISIBLE);
                iv_msg.setVisibility(View.VISIBLE);
                iv_link_man_empty.setVisibility(View.VISIBLE);

                iv_msg.setAlpha(1 - positionOffset);
                iv_msg_empty.setAlpha(positionOffset);
                iv_link_man_empty.setAlpha(1 - positionOffset);
                iv_link_man.setAlpha(positionOffset);
                break;
            case 1:
                // VISIBLE = 0x00000000   INVISIBLE = 0x00000004
                iv_link_man_empty.setVisibility(View.VISIBLE);
                iv_link_man.setVisibility(View.VISIBLE);
                iv_discover.setVisibility(View.VISIBLE);
                iv_discover_empty.setVisibility(View.VISIBLE);

                iv_link_man.setAlpha(1 - positionOffset);
                iv_link_man_empty.setAlpha(positionOffset);
                iv_discover.setAlpha(positionOffset);
                iv_discover_empty.setAlpha(1 - positionOffset);

//                    Log.i(TAG, "onPageScrolled: "+"iv_link_man_empty:"+iv_link_man.getVisibility());
//                    Log.i(TAG, "onPageScrolled: "+"iv_link_man:"+iv_link_man_empty.getVisibility());
//                    Log.i(TAG, "onPageScrolled: "+"iv_msg:"+iv_msg.getVisibility());
//                    Log.i(TAG, "onPageScrolled: "+"iv_msg_empty:"+iv_msg_empty.getVisibility());
//                    Log.i(TAG, "onPageScrolled: "+"iv_discover:"+iv_discover.getVisibility());
//                    Log.i(TAG, "onPageScrolled: "+"iv_discover_empty:"+iv_discover_empty.getVisibility());
                break;
            case 2:
                iv_discover.setVisibility(View.VISIBLE);
                iv_discover_empty.setVisibility(View.VISIBLE);
                iv_me.setVisibility(View.VISIBLE);
                iv_me_empty.setVisibility(View.VISIBLE);

                iv_discover_empty.setAlpha(positionOffset);
                iv_discover.setAlpha(1 - positionOffset);
                iv_me.setAlpha(positionOffset);
                iv_me_empty.setAlpha(1 - positionOffset);
                break;

        }


    }

    @Override
    public void onPageSelected(int position) {

//        Log.i(TAG, "onPageSelected: " + position);

    }

    /**
     * @param state
     * @see ViewPager#SCROLL_STATE_IDLE
     * @see ViewPager#SCROLL_STATE_DRAGGING
     * @see ViewPager#SCROLL_STATE_SETTLING
     */
    @Override
    public void onPageScrollStateChanged(int state) {

        switch (state) {
            case SCROLL_STATE_IDLE:
//                Log.d(TAG, "onPageScrollStateChanged: SCROLL_STATE_IDLE");
                break;
            case SCROLL_STATE_DRAGGING:
//                Log.d(TAG, "onPageScrollStateChanged: SCROLL_STATE_DRAGGING");
                break;
            case SCROLL_STATE_SETTLING:
//                Log.d(TAG, "onPageScrollStateChanged: SCROLL_STATE_SETTLING");
                break;

        }

    }


}
